class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        int size = nums.size(), pre = 0, ans = 0;
        unordered_map<int, int> hash;
        hash[0] = 1;
        for(int i = 0; i < size; ++i)
        {
            pre += nums[i];
            if(hash.count(pre - k)) ans += hash[pre - k];
            ++hash[pre];
        }
        return ans;
    }
};